package yixaun.tim.service.impl;

import lombok.NonNull;
import org.springframework.util.CollectionUtils;
import yixaun.tim.bean.account.MultiImportAccountsVo;
import yixaun.tim.bean.account.TimAccount;
import yixaun.tim.common.TimException;
import yixaun.tim.service.TimAccountService;
import yixaun.tim.service.TimService;
import yixaun.tim.util.JsonUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author pengshiqing
 * @Date: 2019/6/24
 * @Description:
 */

public class TimAccountServiceImpl implements TimAccountService {

    private TimService timService;

    public TimAccountServiceImpl(TimService timService) {
        this.timService = timService;
    }



    @Override
    public void singleImport(TimAccount account) throws TimException {
        String api = "v4/im_open_login_svc/account_import";
        timService.post(api, account);
    }

    /**
     * 帐号批量导入接口
     * @param accounts
     */
    @Override
    public MultiImportAccountsVo multiImport(List<String> accounts) throws TimException {

        if (CollectionUtils.isEmpty(accounts)) {
            throw new TimException(0,"accounts 不能为空");
        }
        if (accounts.size() > 100) {
            throw new TimException(0, "批量发消息目标帐号超过100，请减少 To_Account 中目标帐号数量");
        }

        String api = "v4/im_open_login_svc/multiaccount_import";

        Map<String, List<String>> body = new HashMap<>();
        body.put("Accounts", accounts);

        String jsonResult = timService.post(api, body);
        return JsonUtils.fromJson(jsonResult, MultiImportAccountsVo.class);
    }

    /**
     * 帐号登录态失效接口,移除账号
     * @param identifier
     */
    @Override
    public void kick(@NonNull String identifier) throws TimException {
        String api = "v4/im_open_login_svc/kick";
        Map<String, String> kickBody = new HashMap<>();
        kickBody.put("Identifier", identifier);
        timService.post(api, kickBody);
    }
}
